<template>
  <div class="SystemAssistant">
    <el-card shadow="hover">
      <div class="searchBox">
        <el-radio-group v-model="type" fill="#39383D" @change="getList">
          <el-radio-button label="1">系统助手</el-radio-button>
          <el-radio-button label="2">运营助手</el-radio-button>
        </el-radio-group>
        <el-button
          class="btn"
          icon="el-icon-circle-plus-outline"
          @click="addBtn"
          >添加助手</el-button
        >
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="头像">
          <template slot-scope="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.img"
              :preview-src-list="[scope.row.img]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="account" label="账号"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="editBtn(scope.row)"
              plain
              >编辑</el-button
            >
            <el-button type="info" size="mini" @click="delBtn(scope.row)" plain
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="400px">
      <div class="form">
        <span class="txt">ID</span
        ><span class="form_item"
          ><el-input v-model="newId" class="input"
        /></span>
      </div>
      <div class="form">
        <span class="txt">助手类型</span
        ><span class="form_item">
          <el-radio v-model="addType" :disabled="disabled" label="1"
            >系统助手</el-radio
          >
          <el-radio v-model="addType" :disabled="disabled" label="2"
            >运营助手</el-radio
          >
        </span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subMit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  systemAssistantList,
  systemAssistantEdit,
  systemAssistantDel,
} from "@/api/SystemManagement";
export default {
  data() {
    return {
      type: "1",
      tableData: [],
      dialogVisible: false,
      newId: "",
      oldId: "",
      addType: "",
      title: "",
      disabled: false,
    };
  },
  methods: {
    async getList() {
      const res = await systemAssistantList({ type: this.type });
      if (res.code == 0) {
        this.tableData = res.data;
      }
    },
    addBtn() {
      this.title = "添加助手";
      this.oldId = "";
      this.newId = "";
      this.addType = "";
      this.dialogVisible = true;
      this.disabled = false;
    },
    editBtn(obj) {
      this.title = "更换助手";
      this.oldId = obj.id;
      this.addType = this.type;
      this.dialogVisible = true;
      this.disabled = true;
    },
    delBtn(obj) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          systemAssistantDel({ id: obj.id }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async subMit() {
      if (this.newId == "" || this.addType == "") {
        this.$message({
          type: "error",
          message: "ID或类型不能为空！",
        });
        return;
      }
      const res = await systemAssistantEdit({
        oldId: this.oldId,
        newId: this.newId,
        type: this.addType,
      });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "添加成功！",
        });
        this.dialogVisible = false;
        this.getList();
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.SystemAssistant {
  min-height: calc(100vh - 50px);
  padding: 20px;
  .el-card {
    min-height: calc(100vh - 90px);

    height: 100%;
    .searchBox {
      display: flex;
      justify-content: space-between;
    }
  }
  .form {
    margin-bottom: 20px;
    .txt {
      text-align: right;
      display: inline-block;
      width: 80px;
    }
    .form_item {
      margin-left: 20px;
    }
  }
  .dialog-footer {
    display: block;
    text-align: center;
  }
  .input {
    width: 200px;
  }
}
</style>